/**
 * Created by wangguoqing on 2017/11/8.
 */
var params=new Object();
var url=path+"/system/assignPermission/listWaitUserByOrg";


$(function(){
    //设置AjaxLoading
    setAjaxLoading();
    var grid=$("#tt");
    initGrid(grid);
});


//初始化表格和数据
function initGrid(grid) {
    var columns=[
        { field: "userId", title: "ID", hidden: true, align: "left", sortable: false, resizable: true, width: 80},
        { field: "userCode", title: "用户编码", align: "left",halign: "center", sortable: false, resizable: true, width: 100},
        { field: "userName", title: "用户名称", align: "left",halign: "center", sortable: false, resizable: true, width: 150},
        { field: "organization", title: "所属部门", align: "left",halign: "center", sortable: false, resizable: true, width: 280},
        { field: "phoneNo", title: "手机号码", align: "left",halign: "center", sortable: false, resizable: true, width: 100},
        { field: "email", title: "邮箱", align: "left",halign: "center", sortable: false, resizable: true, width: 150}
    ];
    var treeObj = parent.$.fn.zTree.getZTreeObj("tree");
    var nodes = treeObj.getSelectedNodes();
    params.orgCode = nodes[0].id;
    initPageGridWithCheckBox(grid, params, url,columns,"#tb","userId");
    var options = $("#tt").datagrid("options");
    options.onLoadSuccess=function(data){
        $('#tt').datagrid("fixRownumber");
    };

    getPage(grid,params,url);
}

//查询按钮
function btnQuery(){
    //查询之前 移除所有选取行 避免重复选择
    $('#tt').datagrid('clearSelections');

    //去空格
    params.userName =  $("#userName").val().trim();
    params.organization = $("#organization").val().trim();

    var grid=$("#tt");
    getPage(grid,params,url);
}

function submit_Add(){
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        //$.messager.alert("友情提醒", "请先选择人员！", "info");
        $.alert("default", "info", "请先选择人员！");
        return;
    }
    var ids = "";
    $.each(rows,function(i,value){
        ids+=value.userId+",";
    });
    var param = new Object();
    param.ids = ids;
    var treeObj = parent.$.fn.zTree.getZTreeObj("tree");
    var nodes = treeObj.getSelectedNodes();
    param.orgCode = nodes[0].id;
    $.ajax({
        type:"POST",
        url:path+'/system/assignPermission/saveOrgByUser',
        data:param,
        dataType:"json",
        async: false,
        success:function callback(data){
            var analyMsg = data;
            if(analyMsg.code!="0000"){
                //$.messager.alert('友情提醒',analyMsg.msg,'info',function(){});
                $.alert("default", "error", analyMsg.msg);
            }else{
                //$.messager.alert("友情提醒", "分配人员成功。", "info",function(){
                //    parent.btnQuery();
                //    setTimeout("closeLayer()",100);//设置延时，防止IE下出现异常
                //});
                $.alert("update", "info", "分配人员");
            }
        }
    });
}